package com.youle.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.youle.enums.ExceptionEnum;
import com.youle.exception.YLException;
import com.youle.item.pojo.Brand;
import com.youle.item.pojo.ItemCategory;
import com.youle.mapper.BrandMapper;
import com.youle.mapper.ItemCategoryMapper;
import com.youle.utils.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * Created by
 *
 * @author touni
 * @date 2019/4/18
 * @time 18:53
 */
@Slf4j
@Service
public class BrandService {


    @Autowired
    private BrandMapper brandMapper;

    @Autowired
    private ItemCategoryMapper itemCategoryMapper;



    public ResponseEntity<PageResult> showBrandList(String key, Integer page, Integer row, String sortBy, Boolean desc) {
        String orderBy = null;
        if (desc){
            orderBy = sortBy +" desc";
        }
        orderBy = sortBy + " asc";
        PageHelper.startPage(page,row,orderBy);
        List<Brand> brands = null;
        if (StringUtils.isEmpty(key)){
            brands = this.brandMapper.selectAll();
        }else {
            Example example = new Example(Brand.class);
            example.createCriteria().andLike("name","%"+key+"%").orEqualTo("letter",key);
            brands = this.brandMapper.selectByExample(example);


        }


        PageInfo pageInfo = new PageInfo(brands);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setTotalPage(pageInfo.getTotal()/pageInfo.getPageSize());
        pageResult.setItems(pageInfo.getList());

        return ResponseEntity.status(HttpStatus.ACCEPTED).body(pageResult);
    }



    @Transactional
    public ResponseEntity<Void> addBrand(Brand brand, List<Long> ids) {
        //判断是增加还是修改  根据id来判断
        if (brand.getId() != null){
            int flag = brandMapper.updateByPrimaryKeySelective(brand);
            if (1 != flag){
                throw new YLException(ExceptionEnum.UPDATE_BRAND_FAIL);
            }
        }else {
            //增加的处理逻辑
            int brandFlag = this.brandMapper.insert(brand);
            if (1 != brandFlag) {
                throw new YLException(ExceptionEnum.INSERT_BRAND_FAIL);
            }
        }
        //先查询出哪些是已经保存的
        List<Long> cidList = null;
        if (brand.getId() != null){
            cidList = this.brandMapper.selectCategoryIdByBid(brand.getId());
        }
        for(Long cid:ids){
            if (!CollectionUtils.isEmpty(cidList) && cidList.contains(Long.valueOf(cid)) ){
                continue;
            }

            int flag = this.brandMapper.insertIntoCategoryBrand(cid, brand.getId());
            if (1 != flag){
                log.info("插入到categorybrand表失败"+cid+""+brand.getId());  //插入失败可能是已经存在或者是服务器出错，保存到日志
            }
        }
        return ResponseEntity.status(HttpStatus.ACCEPTED).body(null);
    }

    public ResponseEntity<List<ItemCategory>> queryBrandByBid(Long bid) {
        List<ItemCategory> categoryList = this.itemCategoryMapper.queryBrandByBid(bid);
        if (CollectionUtils.isEmpty(categoryList)){
            throw new YLException(ExceptionEnum.NOT_FOUND);
        }

        return ResponseEntity.status(HttpStatus.ACCEPTED).body(categoryList);
    }



    public ResponseEntity<Void> deleteByBid(Long bid) {
        Brand brand = new Brand();
        brand.setId(bid);
        int flag = brandMapper.delete(brand);
        if (1 != flag){ throw new YLException(ExceptionEnum.DELETE_BRAND_FAIL); }
        //删除中间表
        flag = brandMapper.deleteCategoryBrand(bid);
        if (flag <1 ){
            throw new YLException(ExceptionEnum.DELETE_CATEGORYBRAND_FAIL);
        }
        return ResponseEntity.status(HttpStatus.ACCEPTED).body(null);
    }
}
